package com.example.demoapplication;

import jakarta.servlet.http.HttpSession;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Hua YY
 * Date: 2024-11-11
 * Time: 13:10
 */


@RestController
@RequestMapping("/user")
public class LoginController {
    /*
    * 这个类中的login方法是用来检验前端请求中的账号和密码是否正确的
    * */

    @RequestMapping(value = "/login")
    public Boolean login(String userName , String password , HttpSession session){

        //参数校验
        /*
        if(userName == null || userName.length() == 0
                || password == null || password.length() == 0){
            return false;
        }
        */
        if(!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)){
            return false;
        }

        //检验账号密码是否正确,这里内置的账号密码写死了，后面接触数据库之后在加深理解
        if(!"cbro".equals(userName) || !"123456".equals(password)){
            return false;
        }
        session.setAttribute("userName" , userName);
        return true;
    }

    @RequestMapping("/index")
    public String getUserName(@SessionAttribute("userName") String userName){
        return userName;
    }
}
